package com.game.dongzhao.bean;

import java.io.*;

import static com.game.dongzhao.DongZhaoApplication.logger;

/**
 * 玩家
 */
public class Player {
    /**
     * 实习经验值
     */
    public static int practiceExp = 100;

    /**
     * 管理经验值
     */
    public static int manageExp = 0;

    /**
     * 等级
     */
    public static int level;

    /**
     * 检查次数
     */
    private static boolean checkTimes;

    /**
     * 玩家上次玩到的主线地图clock记录
     */
    public static int lastMapClock = 0;

    /**
     * 检查经验值
     * 小于20提供支线 小于0退出
     */
    public static String checkPlayerExp() {
        String check = "";
        if (practiceExp <= 30 && checkTimes) {
            check = "您的经验值已不足20，请选择前往支线任务赚取经验值！游戏即将因为您的经验值归0结束！\n";
            checkTimes = false;
        } else {
            checkTimes = true;
        }
        return check;
    }

    /**
     * 提升实习经验
     *
     * @param num
     */
    public static void increasePracticeExp(int num) {
        practiceExp = practiceExp + num;
    }

    /**
     * 降低实习经验
     *
     * @param num
     */
    public static void declinePracticeExp(int num) {
        practiceExp = practiceExp - num;
        if (practiceExp <= 0) {
            logger.log(System.Logger.Level.INFO, "玩家因经验值为0结束游戏并删档");
            File data = new File("./assert/playerData.dat");
            data.delete();
            System.exit(0);
        }
    }

    /**
     * 提示管理经验值
     *
     * @param num
     */
    public void increaseManageExp(int num) {
        manageExp = manageExp + num;
    }

    /**
     * 降低管理经验
     *
     * @param num
     */
    public void declineManageExp(int num) {
        manageExp = manageExp - num;
    }

    /**
     * 更新等级
     */
    public static void updateLevel() {
        level = practiceExp / 15;
    }

    /**
     * 初始化数值填充
     */
    public static void initializePlayer() {
        checkTimes = true;
        File playerData = new File("./assert/playerData.dat");
        try {
            // 首次运行或者改过自新
            if (!playerData.exists()) {
                OutputStream outData = new FileOutputStream(playerData);
                DataOutputStream outDataStream = new DataOutputStream(outData);
                outDataStream.writeInt(manageExp);
                outDataStream.writeInt(practiceExp);
                level = practiceExp / 10;
                outDataStream.writeInt(level);
                outDataStream.writeInt(lastMapClock);
                outDataStream.close();
                outData.close();
            } else {
                InputStream inData = new FileInputStream(playerData);
                DataInputStream inDataStream = new DataInputStream(inData);
                manageExp = inDataStream.readInt();
                practiceExp = inDataStream.readInt();
                level = inDataStream.readInt();
                lastMapClock = inDataStream.readInt();
                inDataStream.close();
                inData.close();
            }
        } catch (IOException e) {
            logger.log(System.Logger.Level.ERROR, "初始数据文件建立错误");
        }
    }
}
